package report;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
public class UserManagement {
    private static final String FILE_NAME = "log.txt";
    private static final int RECORD_LENGTH = 100;
    public static void registerUser(String username, String password, int age) {
        try (RandomAccessFile file = new RandomAccessFile(FILE_NAME, "rw")) {
            file.seek(file.length());
            writeString(file, username);
            writeString(file, password);
            file.writeInt(age);
        } catch (IOException e) {
            System.out.println("写入用户信息出现错误: " + e.getMessage());
        }
    }
    public static void listAllUsers() {
        try (RandomAccessFile file = new RandomAccessFile(FILE_NAME, "r")) {
            long fileLength = file.length();
            long currentPosition = 0;
            while (currentPosition < fileLength) {
                String username = readString(file);
                String password = readString(file);
                int age = file.readInt();
                System.out.println("用户名: " + username + ", 密码: " + password + ", 年龄: " + age);
                currentPosition = file.getFilePointer();
            }
        } catch (IOException e) {
            System.out.println("读取用户信息出现错误: " + e.getMessage());
        }
    }
    public static void updatePassword(String username, String newPassword) {
        try (RandomAccessFile file = new RandomAccessFile(FILE_NAME, "rw")) {
            long fileLength = file.length();
            long currentPosition = 0;
            boolean found = false;
            while (currentPosition < fileLength) {
                String storedUsername = readString(file);
                String password = readString(file);
                int age = file.readInt();
                if (storedUsername.equals(username)) {
                    found = true;
                    file.seek(file.getFilePointer() - (password.length() + RECORD_LENGTH - username.length()));
                    writeString(file, newPassword);
                    break;
                }
                currentPosition = file.getFilePointer();
            }
            if (!found) {
                System.out.println("没有该用户");
            }
        } catch (IOException e) {
            System.out.println("更新用户密码出现错误: " + e.getMessage());
        }
    }
    private static void writeString(RandomAccessFile file, String str) throws IOException {
        byte[] bytes = str.getBytes();
        if (bytes.length > RECORD_LENGTH) {
            throw new IOException("字符串过长，超出记录长度限制");
        }
        file.write(bytes);
        for (int i = bytes.length; i < RECORD_LENGTH; i++) {
            file.writeByte(' ');
        }
    }
    private static String readString(RandomAccessFile file) throws IOException {
        byte[] buffer = new byte[RECORD_LENGTH];
        file.readFully(buffer);
        int endIndex = 0;
        while (endIndex < RECORD_LENGTH && buffer[endIndex]!= ' ') {
            endIndex++;
        }
        return new String(buffer, 0, endIndex);
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请选择操作：1. 注册用户  2. 查看所有用户  3. 更新用户密码  4. 退出");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符
            switch (choice) {
                case 1:
                    System.out.println("请输入用户名：");
                    String username = scanner.nextLine();
                    System.out.println("请输入密码：");
                    String password = scanner.nextLine();
                    System.out.println("请输入年龄：");
                    int age = scanner.nextInt();
                    registerUser(username, password, age);
                    break;
                case 2:
                    listAllUsers();
                    break;
                case 3:
                    System.out.println("请输入用户名：");
                    String updateUsername = scanner.nextLine();
                    System.out.println("请输入新密码：");
                    String newPassword = scanner.nextLine();
                    updatePassword(updateUsername, newPassword);
                    break;
                case 4:
                    System.out.println("程序已退出");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效的选择，请重新输入");
            }
        }
    }
}